﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Mvvm;
using Prism.Commands;
using System.Windows;
using WpfRedis.Common;
using WpfRedis.Models;
using WpfRedis.Views;
using System.Reflection;
using MS.Common;
using Prism.Ioc;
using Prism.Events;
using Prism.Regions;
using MS.Common.Event;
using Mqtt;
using wpf.Interface;
using System.Collections;
using System.Threading;

namespace WpfRedis.ViewModels
{
    public class AppMainViewModel : VmBase
    {
        /// <summary>
        /// 构造
        /// </summary>
        public AppMainViewModel(IContainerExtension container, IEventAggregator eventAggregator, IRegionManager regionManager) : base(container, eventAggregator, regionManager)
        {
            eventAggregator.GetEvent<MessageBoxEvent>().Subscribe(OnLogoutEvent, ThreadOption.UIThread);
            NavCommand = new DelegateCommand<object>(OnNavCommand);
            CloseCommand = new DelegateCommand<object>(OnCloseCommand);
            MiniCommand = new DelegateCommand<object>(OnMiniCommand);
            MaxCommand = new DelegateCommand<object>(OnMaxCommand);
            //获取登录信息
            User = new UserModel() { LoginName = "张三" };
            //导航条
            MenuList = new List<IconMenu>() {
              new IconMenu() { Name = "预检分诊", Path = "TriageBase", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "整体录入", Path = "BatchVitalSignView", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "床位", Path = "BedView", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "打开网页", Path = "WebView", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "控件测试页", Path = "TestView", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "绘制打印", Path = "DrawNursing", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "床位", Path = "BedView", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "床位", Path = "BedView", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "床位", Path = "BedView", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "床位", Path = "BedView", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "床位", Path = "BedView", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "床位", Path = "BedView", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "床位", Path = "BedView", ICONNAME = "&#xe624;" }
            , new IconMenu() { Name = "床位", Path = "BedView", ICONNAME = "&#xe624;" }
            };

        }

        private MQTTClients mQTTClient;

        private void OnLogoutEvent(object arg)
        {
            // MessageBox.Show("123");
            var IMessage = container.Resolve<IMessage>();
            IMessage.LoadMessage("11");
        }
        public void Init()
        {
            OnNavCommand("TriageBase");
            Hashtable table = new Hashtable();
            
            //mQTTClient = container.Resolve<MQTTClients>();
            //mQTTClient.SubscribeTopics("WJZ");
            //mQTTClient.MessageReceiveds = new MQTTClients.MessageReceived(MqMessageReceived);
        }
        /// <summary>
        /// 收到订阅的mq消息
        /// </summary>
        /// <param name="ClientId"></param>
        /// <param name="Topic"></param>
        /// <param name="Payload"></param>
        /// <param name="QualityOfServiceLevel"></param>
        /// <param name="Retain"></param>
        public void MqMessageReceived(string ClientId, string Topic, string Payload, string QualityOfServiceLevel, bool Retain)
        {
            MessageBox.Show("收到消息：" + Payload);
        }

        #region 属性

        private List<IconMenu> _ListMenu;
        /// <summary>
        /// 导航头集合
        /// </summary>
        public List<IconMenu> MenuList
        {
            get { return _ListMenu; }
            set { _ListMenu = value; }
        }


        private UserModel _User;

        /// <summary>
        /// 登录信息实体
        /// </summary>
        public UserModel User
        {
            get
            {
                return _User;
            }
            set
            {
                _User = value;
                RaisePropertyChanged(nameof(User));
            }
        }

        private string _SerachText;
        /// <summary>
        /// 搜索文本
        /// </summary>
        public string SerachText
        {
            get { return _SerachText; }
            set
            {
                _SerachText = value;
                RaisePropertyChanged(nameof(SerachText));
            }
        }

        private FrameworkElement _Content;
        /// <summary>
        /// 切换内容
        /// </summary>
        public FrameworkElement Content
        {
            get { return _Content; }
            set
            {
                _Content = value;
                RaisePropertyChanged(nameof(Content));
            }
        }


        #endregion

        #region 事件

        public DelegateCommand<object> NavCommand { get; private set; }

        /// <summary>
        /// 关闭登录窗体
        /// </summary>
        public DelegateCommand<object> CloseCommand { get; private set; }
        public DelegateCommand<object> MiniCommand { get; private set; }
        public DelegateCommand<object> MaxCommand { get; private set; }
        #endregion



        #region 控制按钮
        /// <summary>
        /// 导航
        /// </summary>
        /// <param name="obj"></param>
        public void OnNavCommand(object obj)
        {
            
            string navText = obj.ToString();
            //路径导航
            var uri = new Uri(navText, UriKind.Relative);
            regionManager.RequestNavigate(RegionName.ShellMainRegionName, uri);
            //注册了导航可以用字符串导航
            //regionManager.RequestNavigate("ShellMainRegionName", navText);
        }

        /// <summary>
        /// 关闭程序
        /// </summary>
        /// <param name="obj"></param>

        public void OnCloseCommand(object obj)
        {
            if (obj is Window)
            {
                (obj as Window).Close();
                Environment.Exit(0);
            }
        }

        private Window Win = null;
        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="obj"></param>
        public void OnMiniCommand(object obj)
        {
            //int x = 0;
            //new Thread(() => { _ = 1 / x; }).Start();
            if (obj is Window win)
            {
                Win = win;
                Win.WindowState = WindowState.Minimized;
            }
        }

        /// <summary>
        /// 最大化
        /// </summary>
        public void OnMaxCommand(object obj)
        {
            if (obj is Window win)
            {
                Win = win;
                if (Win.WindowState == WindowState.Maximized)
                    Win.WindowState = WindowState.Normal;
                else
                    Win.WindowState = WindowState.Maximized;
            }
        }

        #endregion

    }
}
